<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Region extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model("User_model","user");
        $this->load->library('Easemob');
    }

    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：手机注册获取短信验证码
     *         首先验证该手机号是否注册，然后调用容联云接口发送短信验证，60秒有效期
     * @action：/region/sendSMS
     * @method：GET
     * @param：reginMobile             类型：STRING，必须：YES ，描述：手机号码
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */
    public function sendSMS(){
        // file_put_contents("mylog.txt", file_get_contents('php://input')."注册\r\n",FILE_APPEND);
        $params['reginMobile'] = $this->queryData['reginMobile'];
        $validate = new Validate([
            'reginMobile' => 'require|string',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        //判断用户是否存在
        if($this->user->check_user($params['reginMobile'])){
            $this->errorCode = 50007;
            $this->createApi();exit();
        };

        //判断当天本用户登录次数 不能超过三次
        /*$maxSendCnt = $this->config->item('maxSendCnt');
        if($this->user->checkSendCnt($reginMobile, $maxSendCnt) == false){
            $this->errorCode = API_ERROR_SMS_OVER;
            $this->createApi();exit();
        }*/

        require_once(APPPATH.'third_party/yuntongxun/SendTemplateSMS.php');
        $mcode = strtolower(random_string('numeric',6));
        //验证码生成,并执行发送短信，60秒有效期
        $datas = array($mcode,10);
        $res = sendTemplateSMS($params['reginMobile'],$datas,133051);
        $data = array("mcode"=>$mcode,"expiretime"=>time()+600);
        $this->user->saveSmsStatus($params['reginMobile'],$data,$maxSendCnt);
        if(empty($res)){
            $this->errorCode = 60007;
        }
        $this->createApi();
    }


    

    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：执行手机注册功能
     *         1）首先验证验证码有效期
     *         2）判断验证码是否正确
     *         3）执行添加用户到本地数据库（成功后将用户名和密码同步到环信）
     * @action：/region/sendSMS
     * @method：GET
     * @param：loginName             类型：STRING，必须：YES ，描述：手机号码
     * @author:：KevinBai
     *------------------------------------------------------------------------------------------------------------------
     */
    public function do_region(){
        $json_content = $this->getFileContent();
        $params['username'] = $json_content['loginName'];
        $params['password'] = $json_content['password'];
        $params['mcode'] = $json_content['mcode'];
        $validate = new Validate([
            'username' => 'require|string',
            'password'=>'require|string',
            'mcode'=>'require|string'
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $mcodeInfo = $this->user->get_mcodeInfo($params['username']);
        //判断验证码是否正确
        if($params['mcode'] != $mcodeInfo['mcode']){
            $this->errorCode = 60004;
            $this->createApi();exit();
        }
        //判断是否过期
        if($mcodeInfo['expiretime'] < time()) {
            $this->errorCode = 60002;
            $this->createApi();exit;
        }
        //判断用户是否存在
        if($this->user->check_user($params['username'])){
            $this->errorCode = 50007;
            $this->createApi();exit();
        };
        $data = array(
            'username'=>$params['username'],
            'password'=>$params['password'],
        );
        $res = $this->user->add_user($data);
        if(empty($res)){
            $this->errorCode = 60003;
            $this->createApi();exit;
        }
        
        $points = $this->setPoint(1);
        $data = array(
            'AccountID'=>$res['AccountID'],
            'createtime'=>date('Y-m-d',time()),
            'points'=>$points,
            'type'=>1
            );
        $this->load->model("Point_model","point");
        $this->point->addPoints($res['AccountID'],$data);
        $this->createApi($res);
    }


    public function protocol(){
        $protocol = $this->user->getProtocol();
        $this->createApi($protocol);

    }


}

/* End of file region.php */
/* Location: ./application/controllers/region.php */
